package com.tledu.springboot19.controller;

import com.tledu.springboot19.spider.XLNbaPageProcessor;
import com.tledu.springboot19.spider.XLNbaPipeline;
import com.tledu.springboot19.spider.XLNewsPageProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Spider;

import java.util.HashMap;
import java.util.Map;

@Controller
public class SpiderController {
    @Autowired
    private XLNbaPageProcessor xlNbaPageProcessor;

    @Autowired
    private XLNewsPageProcessor xlNewsPageProcessor;

    @Autowired
    private XLNbaPipeline xlNbaPipeline;

    @RequestMapping("/start/xl/nba")
    // 加上这个注解之后，就代表，直接返回就是对应的文本内容，就不会在去templates找页面了
    @ResponseBody
    public String startXLNba() {
        Spider.create(xlNbaPageProcessor)
                .addUrl("https://sports.sina.com.cn/nba/")
                .thread(5)
                // 使用webMagic默认实现
                .addPipeline(xlNbaPipeline)
                .run();
        return "startSpider";
    }

    @RequestMapping("/start/xl/news")
    // 加上这个注解之后，就代表，直接返回就是对应的文本内容，就不会在去templates找页面了
    @ResponseBody
    public String startXLNews() {
        Request request = new Request();
        request.setUrl("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2509&k=&num=50&page=1&r=0.7528406726388162");
        // 设置当前的页码
        Map<String, Object> map = new HashMap<>();
        map.put("pageNum", 1);
        // 把这个页码添加到Request里面
        request.setExtras(map);

        Spider.create(xlNewsPageProcessor)
                .addRequest(request)
                .thread(5)
                // 使用webMagic默认实现
                .addPipeline(xlNbaPipeline)
                .run();
        return "startSpider";
    }
}
